/*
 * Copyright 2009 eBusinessInformation
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package com.excilys.ymca.inject;

import org.apache.http.client.HttpClient;
import org.apache.http.impl.client.DefaultHttpClient;
import org.codehaus.jackson.map.DeserializationConfig;
import org.codehaus.jackson.map.ObjectMapper;

import roboguice.config.AbstractAndroidModule;
import roboguice.inject.ActivityProvider;

import com.excilys.ymca.activity.ApplicationUpdateListener;
import com.excilys.ymca.activity.UpdateApplicationActivity;
import com.excilys.ymca.service.ContactService;
import com.excilys.ymca.service.zencontact.ZenContactService;
import com.excilys.ymca.util.LoggedInHttpClient;
import com.excilys.ymca.util.LoggedInHttpClientProvider;

public class YMCAModule extends AbstractAndroidModule {

    @Override
    protected void configure() {

        /*
         * On spécifie quelle est l'implémentation de IContactListService
         * utilisée
         */
        bind(ContactService.class).to(ZenContactService.class);
        // bind(ContactService.class).to(RemoteFileContactService.class);

        /*
         * On spécifie quelle est l'implémentation de ApplicationUpdateListener
         * utilisée
         */
        bind(ApplicationUpdateListener.class).to(UpdateApplicationActivity.class);

        /*
         * Unchecked : permet de binder UpdateApplicationActivity sur l'activité
         * courante. Jettera une ClassCastExceptin si le context courant n'est
         * pas une UpdateApplicationActivity.
         * 
         * Sans cette ligne de configuration, Guice va créer une nouvelle
         * instance de UpdateApplicationActivity à chaque demande, plutôt que
         * d'utiliser le context courant.
         * 
         * Il s'agit d'un workaround.
         * 
         * A ce sujet, lire :
         * http://code.google.com/p/roboguice/issues/detail?id=19
         */

        bind((Class) UpdateApplicationActivity.class).toProvider(ActivityProvider.class);

        bind(HttpClient.class).to(DefaultHttpClient.class);
        bind(HttpClient.class).annotatedWith(LoggedInHttpClient.class).toProvider(LoggedInHttpClientProvider.class);

        // Configuration du mapping JSON
        ObjectMapper mapper = new ObjectMapper();
        DeserializationConfig config = mapper.getDeserializationConfig();
        config.set(DeserializationConfig.Feature.FAIL_ON_UNKNOWN_PROPERTIES, false);

        bind(ObjectMapper.class).toInstance(mapper);

    }
}
